<?php
/*
 * Created on May 25, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Fck_files_model extends CI_Model
{
	/**
	 * @var unknown_type
	 */
	var $id  ;  
	/**
	 * @var unknown_type
	 */
	var $category_parent_id  ;  
	/**
	 * @var unknown_type
	 */
	var $name_vi  ; 
	/**
	 * @var unknown_type
	 */
	var $name_en  ;  
	/**
	 * @var unknown_type
	 */
	var $description_vi;
	/**
	 * @var unknown_type
	 */
	var $description_en;
	/**
	 * @var unknown_type
	 */
	var $picture;
	/**
	 * @var unknown_type
	 */
	var $active;
	/*
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
	}

        function Fck_files_model()
	{
		parent::__construct();
	}
	
	function insertGroupUser($user_id, $group_id, $permission_id, $color_id, $active){
		$sql = "INSERT IGNORE INTO user_group (user_id, group_id, permission_id, color_id, active) VALUES ($user_id, $group_id, $permission_id, $color_id, $active)";
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function create($categories){
		if(!is_array($categories))
			$categories = array($categories);
		
		$sql = "INSERT INTO `gtb_fck_files` " .
				"(`file_url`, `item_id`, `type` ) " .
				"VALUES";
		for($i = 0; $i < count($categories); $i++){
			$category = $categories[$i];
			$sql .= "(" .
					 $this->db->escape($category->file_url).", " .
					intval($category->item_id).", " .
					 $this->db->escape($category->type).
					"),";
				
		}
		$sql = substr($sql, 0, strlen($sql)-1);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
		//return $this->db->insert_id(); 
	}
	
	function update($category){
		 $sql = "UPDATE gtb_files SET " .
				"id_category = " .  intval($category->id_category) . "," .
				"vi_alias = " .  $this->db->escape($category->vi_alias) . "," .
				"en_alias = " .  $this->db->escape($category->en_alias) . ", " .
				"file_name = " .  $this->db->escape($category->file_name) . ", " .
				"active = " .  intval($category->active) . ", " .
				"upd_date = " .  $this->db->escape($category->upd_date) .
				"WHERE id_file = " .intval($category->id_file);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}

	
	function delete($ids){
		if(!is_array($ids)) $files_name = array($ids);
		$ids = implode(',', $ids);
		
		$sql = "DELETE FROM gtb_fck_files WHERE id IN ($ids)";
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
		
	function getAll($number_record, $offset, $id_category =  0){
		if(!is_array($id_category))
		$sql = "SELECT *
				FROM gtb_files " .
				" WHERE id_category= ". intval($id_category). " OR 0 = ". intval($id_category). " ORDER BY id_file DESC" .
				" LIMIT ". intval($offset) ."," . intval($number_record);
		elseif(is_array($id_category)){
			
			$sql = "SELECT *
				FROM gtb_files " .
				" WHERE id_category IN (". implode(',',$id_category). ") ORDER BY id_file DESC" .
				" LIMIT ". intval($offset) ."," . intval($number_record);
				
		}
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getAllByItemId($item_id, $type){
		
		$sql = "SELECT *
				FROM gtb_fck_files " .
				" WHERE item_id = (". intval($item_id). ") AND type='$type'" ;
				
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function countAll($id_category){
		$result = null;
		if(!is_array($id_category))
			$sql = "SELECT COUNT(*) as num_record 
					FROM gtb_files " .
					"WHERE id_category= ". intval($id_category). " OR 0 = ". intval($id_category);
		elseif(is_array($id_category))
			$sql = "SELECT COUNT(*) as num_record
					FROM gtb_files " .
					" WHERE id_category IN (". implode(',',$id_category). ")";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	if($result)
			return $result[0]->num_record;
			
		return 0;
	}
	
	
	function getById($file_id){
		
		$sql = "SELECT *
				FROM gtb_files 
				WHERE id_file = " . intval($file_id);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
		
//	
}

// EOF

